﻿using System;
using System.Linq;
using DAL;
using Models;

namespace BLL
{
    public class PointService:BaseService
    {
        public PointInfo GetPoint(int userId, int objId, int objTypeId)
        {
            var query = from q in db.Points
                    where q.PostUserId == userId && q.ObjId == objId && q.ObjType == objTypeId
                    select new PointInfo()
                        {
                            PointScore = q.PointScore,

                        };
            return query.SingleOrDefault();

        }

        public void PostPointScore(int userId, int objId, ObjType objTypeId, int pointScore)
        {
            var s = new Point();
            s.CreatedOn = DateTime.Now;
            s.PointScore = pointScore;
            s.ObjId = objId;
            s.ObjType = (int)objTypeId;
            s.Deleted = false;
            s.PostUserId = userId;

            db.Points.InsertOnSubmit(s);
            db.SubmitChanges();

            SyncPoint(objId, objTypeId);
        }

        /// <summary>
        /// 视频类评分总分=所有普通用户评分的平均值+所有特殊用户评分的平均值÷2（分数可后台修改）
        /// </summary>
        private void SyncPoint(int objId, ObjType objTypeId)
        {
            if (objTypeId == ObjType.微力十足)
            {
                var normalScoreList = (from p in db.Points
                                       join user in db.Users on p.PostUserId equals user.Id
                                       where
                                           p.ObjId == objId && p.ObjType == (int) objTypeId && user.Deleted == false &&
                                           user.UserType == (int) UserType.Normal
                                       select p.PointScore).ToList();

                var vipScoreList = (from p in db.Points
                                    join user in db.Users on p.PostUserId equals user.Id
                                    where
                                        p.ObjId == objId && p.ObjType == (int) objTypeId && user.Deleted == false &&
                                        user.UserType == (int) UserType.Vip
                                    select p.PointScore).ToList();

                float vipAverage = -1;
                float normalAverage = -1;
                if (normalScoreList.Count > 0)
                {
                    normalAverage = ((float)normalScoreList.Sum()) / normalScoreList.Count;
                }
                if (vipScoreList.Count > 0)
                {
                    vipAverage = ((float)vipScoreList.Sum()) / vipScoreList.Count;
                }

                // 默认情况下，没有评分的时候，评分设为5.
                float finalScore = 5;
                if (vipAverage <= 0 && normalAverage > 0)
                {
                    finalScore = normalAverage;
                }
                else if (normalAverage <= 0 && vipAverage > 0)
                {
                    finalScore = vipAverage;
                }
                else if (normalAverage > 0 && vipAverage > 0)
                {
                    finalScore = (normalAverage + vipAverage) / 2;
                }

                var video = VideoInfoService.GetVideoInfoByID(objId);
                video.Count += 1;
                video.Point = finalScore;
                VideoInfoService.ModifyVideoInfo(video);
            }
        }
    }
}